package com.accp.crm.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.SessionFactory;

import com.accp.crm.dao.CommonDAO;
import com.accp.crm.entity.SysRight;
import com.accp.crm.entity.SysUser;

public class RoleFilter implements Filter {
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) resp;
		HttpSession session = request.getSession();
		SysUser user=(SysUser)session.getAttribute("USER");
		if(user==null){
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			return ;
		}
        String uri=request.getRequestURI();
		String path=uri.substring(request.getContextPath().length(),uri.indexOf(".do"));
		List list=user.getRight();
		Boolean i=list.contains(path);
		if(i==false){
			response.sendRedirect(request.getContextPath()+"/noPermission.jsp");
			return ;
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
